package vip.meeet.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EncryptFileUtil {

    private static final String RESOURCE_DIR;

    private static Map<String,String> endata;

    static {
        URL url = EncryptFileUtil.class.getResource("/");
        String path = url.getFile().substring(1);
        if(path.contains("target/test-classes")){
            RESOURCE_DIR = path.replace("target/test-classes/", "src/main/resources/");
        }else {
            RESOURCE_DIR = path.replace("target/classes/", "src/main/resources/");
        }
    }

    /**
     *
     * @param filePath classpath中的相对路径 data/sensitive
     * @param pass
     * @return
     * @throws Exception
     */
    public static <T> T getBean(String filePath,String pass,Class<T> clazz) throws Exception {
        File file = getResourceFile(filePath);
        String content = EncryptUtil.decrypt(file, pass);
        return JSON.parseObject(content,clazz);
    }

    public static <T> List<T> getBeans(String filePath, String pass, Class<T> clazz) throws Exception {
        File file = getResourceFile(filePath);
        String content = EncryptUtil.decrypt(file, pass);
        return JSON.parseArray(content,clazz);
    }

    public static <T> void setBean(String filePath,T bean,String pass) throws Exception {
        EncryptUtil.encryptContent(JSON.toJSONString(bean),RESOURCE_DIR + filePath, pass);
    }

    public static <T> void setBeans(String filePath,List<T> beans,String pass) throws Exception {
        EncryptUtil.encryptContent(JSON.toJSONString(beans),RESOURCE_DIR + filePath, pass);
    }

    public static void setContent(String filePath,String content,String pass) throws Exception {
        EncryptUtil.encryptContent(content,RESOURCE_DIR + filePath, pass);
    }

    public static String getContent(String filePath,String pass) throws Exception {
        File file = getResourceFile(filePath);
        return EncryptUtil.decrypt(file, pass);
    }

    private static File getResourceFile(String filePath){
        return new File(RESOURCE_DIR + filePath);
    }

    private static String readResouceContent(String filePath) throws IOException {
        File file = new File(RESOURCE_DIR + filePath);
        return FileUtils.readFileToString(file, Charset.defaultCharset());
    }

    private static void writeResouceContent(String filePath,byte[] content) throws IOException {
        Path path = Paths.get(RESOURCE_DIR + filePath);
        Files.write(path,content, StandardOpenOption.CREATE);
    }

    public static String getEncryptValue(String key) throws Exception {
        if(endata == null){
            initEnData();
        }
        return endata.get(key);
    }

    private static void initEnData() throws Exception {
        File file = new File("G:\\worker\\data\\ps");
        String pass = FileUtils.readFileToString(file, Charset.defaultCharset());
        endata = EncryptFileUtil.getBean("data/sensitive", pass, HashMap.class);
    }


}
